﻿using System.Data;
using System.Data.SqlClient;
using QuanLyThuVien.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyThuVien.Supporter;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng CT_PHIEUTRA
    //----------------------------------------------------------------------
    public class DALCT_PhieuTra
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALCT_PhieuTra()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành CT_PhieuTra
        //----------------------------------------------------------------------
        public DTOCT_PhieuTra ConvertToCT_PhieuTra(DataRow row)
        {
            DTOCT_PhieuTra ctptr = new DTOCT_PhieuTra();
            ctptr.MaPhieuTra = row["MaPhieuTra"].ToString();
            ctptr.CTPhieuMuon.MaCuonSach = row["MaCuonSach"].ToString();
            ctptr.CTPhieuMuon.MaPhieuMuon = row["MaPhieuMuon"].ToString();
            try
            {
                ctptr.SoNgayMuon = Int32.Parse(row["SoNgayMuon"].ToString());
            }
            catch { }
            try
            {
                ctptr.TienPhat = Double.Parse(row["TienPhat"].ToString());
            }
            catch { }

            return ctptr;
        }

        //----------------------------------------------------------------------
        //Desc: Lấy danh sách CT_PhieuTra từ PhieuTra
        //----------------------------------------------------------------------
        public List<DTOCT_PhieuTra> LayDanhSachCT_PhieuTra(string maphieutra)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spPhieuTra_LayDanhSachCTPT";
            SqlParameter sqlprMaPhieuTra = new SqlParameter("@MaPhieuTra", SqlDbType.VarChar, 15) { Value = maphieutra };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaPhieuTra);

            if (data == null)
                return null;
            List<DTOCT_PhieuTra> list = new List<DTOCT_PhieuTra>();
            foreach (DataRow row in data.Rows)
                list.Add(ConvertToCT_PhieuTra(row));
            return list;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật CT_PHIEUTRA
        //----------------------------------------------------------------------
        public bool CapNhat(DTOCT_PhieuTra ctphieutra)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spCT_PhieuTra_CapNhatCTPT";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprMaPhieuTra = new SqlParameter("@MaPhieuTra", SqlDbType.VarChar, 15) { Value = ctphieutra.MaPhieuTra };
            SqlParameter sqlprMaCuonSach = new SqlParameter("@MaCuonSach", SqlDbType.VarChar, 15)
            { Value = ctphieutra.CTPhieuMuon.MaCuonSach };
            SqlParameter sqlprMaPhieuMuon = new SqlParameter("@MaPhieuMuon", SqlDbType.VarChar, 15)
            { Value = ctphieutra.CTPhieuMuon.MaPhieuMuon };
            SqlParameter sqlprSoNgayMuon = new SqlParameter("@SoNgayMuon", SqlDbType.Int) { Value = ctphieutra.SoNgayMuon };
            SqlParameter sqlprTienPhat = new SqlParameter("@TienPhat", SqlDbType.Money) { Value = ctphieutra.TienPhat };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprMaPhieuTra,
                sqlprMaCuonSach, sqlprMaPhieuMuon, sqlprSoNgayMuon, sqlprTienPhat);
        }
    }
}
